package com.onlyphoto.onlymall.mvp.view.binder

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.bean.response.CouponBean
import com.onlyphoto.onlymall.events.ReceiveEvent

import me.drakeet.multitype.ItemViewBinder
import org.greenrobot.eventbus.EventBus

/**
 * Created by gaochujia on 2019-08-25.
 */
class CouponBinder : ItemViewBinder<CouponBean, CouponBinder.ViewHolder>() {

    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
        val root = inflater.inflate(R.layout.item_coupon, parent, false)
        return ViewHolder(root)
    }

    override fun onBindViewHolder(viewHolder: ViewHolder, couponBean: CouponBean) {
        viewHolder.update(couponBean)

    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var price: TextView? = null
        var condition: TextView? = null
        var couponName: TextView? = null
        var date: TextView? = null
        var btnFeatures: TextView? = null

        init {
            price = itemView.findViewById(R.id.price)
            condition = itemView.findViewById(R.id.condition)
            couponName = itemView.findViewById(R.id.coupon_name)
            date = itemView.findViewById(R.id.date)
            btnFeatures = itemView.findViewById(R.id.btn_features)
        }

        @SuppressLint("SetTextI18n")
        fun update(couponBean: CouponBean) {
            price?.text = "¥ " + couponBean.value
            condition?.text = "满" + couponBean.condition + "可用"
            couponName?.text = couponBean.title
            date?.text = couponBean.start_ymd + "-" + couponBean.end_ymd
            if (couponBean.isStore) {
                btnFeatures?.text = "领取"
                btnFeatures?.setOnClickListener{
                    EventBus.getDefault().post(ReceiveEvent(couponBean.id, couponBean.end_tm))
                }
            }
        }
    }
}
